import { createStore } from "vuex";


export const store = createStore({
  state: {
    aiScoreData: null,
  },
  mutations: {
    setAiScoreData(state, data) {
      state.aiScoreData = data;
    },
  },
  actions: {
    updateAiScoreData({ commit }, data) {
      commit('setAiScoreData', data);
    },
  },
});

export default createStore({
  state() {
    return {
      // 路由导航start
      // 缓存组件页面
      catch_components: [],
      // 当前选中的菜单 - 默认选择首页
      activePath: "/index",
      // 菜单项 - 默认需要展示的页面()
      tabList: [
        {
          path: "/index",
          label: "首页",
          name: "index",
          fullPath: "/index",
        },
      ],
      // 路由导航end
    };
  },
  // 更改vuex的store中状态的唯一方法 - 同步操作
  mutations: {
    // 路由导航start
    //清空vuex数据
    clearVUEX(state) {
      state.catch_components = [];
      state.activePath = "/index";
      state.tabList = [
        {
          path: "/index",
          label: "首页",
          name: "index",
          fullPath: "/index",
        },
      ];
    },
    // 路由导航end
    selectMenu(state, submenu) {
      // 首页就是 wellcome   也就是 home
      if (submenu.name === "index") {
        submenu.path = "/index";
        submenu.fullPath = "/index";
      }
      // 当前选中菜单
      const activePath = submenu.name;
      // 历史已选中菜单列表
      const oldTabList = state.tabList;
      // 将菜单信息添加到tablist - 添加时判断是否已有该路由标签
      const result = oldTabList.some((item) => {
        if (item.name === activePath) {
          if (item.fullPath !== submenu.fullPath) {
            item.fullPath = submenu.fullPath;
          }
          return true;
        }
      });
      // 如果不包含该对象，则添加
      if (!result) {
        oldTabList.push({
          path: submenu.path,
          name: submenu.name,
          label: submenu.label,
          fullPath: submenu.fullPath,
        });
      }
      // 重新赋值标签路由和当前选中菜单
      state.activePath = activePath;
      state.tabList = oldTabList;
    },
    // 添加keepalive缓存
    addKeepAliveCache(state, val) {
      // 如果是首页不缓存
      if (val === "index") {
        return;
      }
      // 添加时判断，如果该组件已存在，便不添加
      if (!state.catch_components.includes(val)) {
        state.catch_components.push(val);
      }
    },
    // 删除keepalive缓存
    removeKeepAliveCache(state, val) {
      state.catch_components = state.catch_components.filter(
        (item) => item !== val
      );
    },
    //关闭菜单
    closeTab(state, val) {
      // 重新赋值
      state.activePath = val.activePath;
      state.tabList = val.tabList;
    },
    // 点击标签选择菜单
    changeMenu(state, val) {
      state.activePath = val;
    },
  },
  actions: {},
});
